package com.suotuo.module.sys.service.impl;

import com.suotuo.module.sys.dao.SwiperDAO;
import com.suotuo.module.sys.entity.Swiper;
import com.suotuo.module.sys.service.SwiperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author liulubingjava@163.com
 * @date 2019/1/22 13:51
 */
@Service
public class SwiperServiceImpl implements SwiperService {
    @Autowired
    private SwiperDAO swiperDAO;

    @Override
    public Page<Swiper> list() {
        PageRequest pageRequest = PageRequest.of(0,10);
        return swiperDAO.findAll(pageRequest);
    }

    @Override
    public Swiper getSwiper(String swiperId) {
        return swiperDAO.findById(swiperId).get();
    }

    @Override
    public void addSwiper(Swiper swiper) {
        swiperDAO.save(swiper);
    }

    @Override
    public void update(Swiper oldSwiper, String swiperId) {
        Swiper newSwiper = swiperDAO.findById(swiperId).get();
        newSwiper.setGameId(oldSwiper.getGameId());
        newSwiper.setSwiperPic(oldSwiper.getSwiperPic());
        newSwiper.setSwiperTitle(oldSwiper.getSwiperTitle());
        swiperDAO.saveAndFlush(newSwiper);
    }

    @Override
    public void deleteOne(String swiperId) {
        swiperDAO.deleteById(swiperId);
    }
}
